package routers

import (
	"net/http"
	"shqsoft.cn/pasnj/base"
	"shqsoft.cn/pasnj/entity"

	"github.com/labstack/echo"
)

func SaveIndiWorkFlowRouter(c echo.Context) error {
	dao, e, err := getDaoAndEntity(c)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	v, ok := e.(*entity.TIndiDiviWorkflow)
	if !ok {
		return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败！")
	}

	rows, err := base.DB.Raw("SELECT INCN FROM T_INDI_CLIE WHERE IDNO=?", v.IDNO).Rows()
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	var cunmStr string
	if rows.Next() {
		rows.Scan(&cunmStr)
	}
	if cunmStr != v.NAME {
		return echo.NewHTTPError(http.StatusBadRequest, "客户名称和证件号码不正确或不匹配！")
	}
	if v.ID == 0 {
		_, err = dao.Add(e)
	} else {
		_, err = dao.Update(e)
	}
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	return c.JSON(http.StatusOK, "OK")
}
